mysqli_*()
s.// mysqli_connect(host, MySQL_username, MySQL_password, db_name) $conn = mysqli_connect('localhost', 'mySQLusername', 'topmostsecretpassword', 'C354_test'); // not 'cs.tru.ca'; 'C354_test' is a db name. if (mysqli_connect_errno()) // or if (!$conn) echo "Failed to connect to C354_test: " . mysqli_connect_error(); else echo "Succeeded to connect to C354_test"; mysqli_close($conn);
// host, user name, password, db name $conn = mysqli_connect('localhost', ???, ???, 'C354_test'); if (mysqli_connect_errno()) echo 'Failed to connect: ' . mysqli_connect_error(); else { $sql = 'CREATE TABLE Persons( SSN INT PRIMARY KEY, FirstName VARCHAR(30) NOT NULL, LastName VARCHAR(30) NOT NULL, Age INT )'; if (mysqli_query($conn, $sql)) echo 'Table Persons created'; else echo 'Error creating table: ' . mysqli_error($conn); mysqli_close($conn); }
$conn = mysqli_connect('localhost', ????); if (mysqli_connect_errno()) echo 'Failed to connect: ' . mysqli_connect_error(); else { $sql = "insert into Persons values ('Dave', 'Smith', 23)"; // Do you remember all the columns? if (mysqli_query($conn, $sql)) echo 'Table Persons updated'; else echo 'Error updating table: ' . mysqli_error($conn); $sql = "??? ??? Persons ??? (999888777, 'Tom', ???, 18)"; // Tom Davis ???(???, $sql); $sql = "??? ??? Persons ??? (888777666, ???, 'Brown', 21)"; // John Brown ???($conn, ???); $sql = "??? ??? Persons ??? (888777666, 'John', 'Black', 21)"; // What if you insert similar values again? ???(???, ???); ???($conn); }
$conn = ????; if (mysqli_connect_errno()) echo 'Failed to connect: ' . mysqli_connect_error(); else { $sql = "select FirstName, LastName from Persons"; // or, "select * from Persons" // Column names are NOT strings. // $sql = "select FirstName, LastName // from Persons"; // Can you use multiple lines for a string value? $result = ????; // The select query result is NOT an array of selected rows. echo mysqli_num_rows($result) . '<br>'; // if (???($result) > 0) // if the number of rows is > 0 while ($row = mysqli_fetch_assoc($result)) // mysqli_fetch_assoc() returns an associative array, and mysqli_fetch_array() returns an indexed array. echo $row['FirstName'] . " " . ???['LastName'] . '<br>'; // $row['FirstName'] . " " . $row['LastName'] . " " . $row['Age'] . '<br>'; // What if you try to print Age? mysqli_close($conn); }
$users = array(); // or, $users = []; while ($row = mysqli_fetch_assoc($result)) $users[] = $row; // or array_push(...)? // or $users = []; $i = 0; while ($row = mysqli_fetch_assoc($result)) $users[$i++] = $row; // $users is a linear array of associative arrays.
$conn = mysqli_connect('???', '???', '???', '???'); if (mysqli_connect_errno()) echo 'Failed to connect to ???: ' . mysqli_connect_error(); else { $sql = "select FirstName, LastName ??? Persons where (Age > 20)"; // Which rows will be selected? $result = ???($conn, $sql); while ($row = ???($result)) // Fetch an associative array echo $row['FirstName'] . " " . $row['LastName']; mysqli_close($conn); }
$conn = mysqli_connect('???', '???', '???', '???'); if (mysqli_connect_errno()) echo 'Failed to connect to ???: ' . mysqli_connect_error(); else { $sql = "select FirstName, LastName from Persons where (Age < 30 and Age > 10) // Comment here? // Not a good idea // because this comment becomes a part of the string. order by Age DESC"; // DESC or ASC $result = mysqli_query($conn, $sql); $persons = []; $i = 0; while ($row = mysqli_fetch_assoc($result)) { $persons[$i] = $row; $i++; } for ($i = 0; $i < count($persons); $i++) { foreach($persons[$i] as $k => $v) echo $persons[$i][$k] . " => " . $persons[$i][$v] . "<br>"; } mysqli_close($conn); }